package com.thinkfit.jy.activity;

import android.view.Gravity;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.common.lib.bean.ProceduralTrainBean;
import com.common.lib.constant.Constants;
import com.common.lib.constant.EventBusEvent;
import com.common.lib.fragment.BaseFragment;
import com.common.lib.manager.DataManager;
import com.thinkfit.jy.R;
import com.thinkfit.jy.fragment.ProceduralTrainingListFragment;
import com.thinkfit.jy.serialport.SerialPortManager;
import com.thinkfit.jy.utils.DeviceTypeUtils;
import com.thinkfit.jy.utils.MotionDataUtils;
import com.thinkfit.jy.utils.SportsStatusBarViewUtils;
import com.thinkfit.jy.utils.ThemeUtils;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.HashMap;

public class ProceduralTrainingActivity extends MainActivity{
    private ArrayList<BaseFragment> mFragments;
    private int mCurrentItem = 0;
    private int mCurrentStyle;

    ArrayList<TextView> tvs;

    ProceduralTrainBean proceduralTrainBean;

    LinearLayout ll;
    RelativeLayout rlMain;
    int type;
    @Override
    protected int getLayoutId() {
        return R.layout.activity_procedural_training;
    }

    @Override
    public void init() {
        resetDate();
        setViewsOnClickListener(R.id.ll_sport_back,R.id.ivHome, R.id.ivSound, R.id.ivAvatar, R.id.ivSetting,R.id.ivBluetooth,R.id.llStart);
        ll = findViewById(R.id.ll);
        rlMain = findViewById(R.id.rl);
//        if(DeviceTypeUtils.connectDeviceType() == 0){
//            showStartBtn(BluetoothOrder.getInstance().isRun());
//
//        }else {
//            showStartBtn(SerialPortManager.getInstance().isRun());
//            resetInfo(SerialPortManager.getInstance().getCurrentInfo());
//        }
        showStartBtn(MotionDataUtils.getInstance().isRun());
        resetInfo(MotionDataUtils.getInstance().getCurrentInfo());

        setViewsOnClickListener(R.id.tv0, R.id.tv1, R.id.tv2, R.id.tv3);
        type = getIntent().getExtras().getInt(Constants.BUNDLE_EXTRA);

        getPresenter().getVideoList(DeviceTypeUtils.getDeviceType());


    }

    @Override
    public void onResume() {
        super.onResume();
        SportsStatusBarViewUtils.getInstance().isMain(false);
        rlMain.setBackgroundResource(ThemeUtils.getInstance().getMainTheme());
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onReceive(HashMap<String, Object> map) {
        if (map == null) {
            return;
        }
        if (map.containsKey(EventBusEvent.TIME_CHANGE)) {
            resetDate();
        }else {
            super.onReceive(map);
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ll_sport_back:
                finish();
                break;
            case R.id.tv0:
                ViewPager viewPager = findViewById(R.id.viewPager);
                viewPager.setCurrentItem(0);
                break;
//            case R.id.tv1:
//                viewPager = findViewById(R.id.viewPager);
//                viewPager.setCurrentItem(1);
//                break;
//            case R.id.tv2:
//                viewPager = findViewById(R.id.viewPager);
//                viewPager.setCurrentItem(2);
//                break;
//            case R.id.tv3:
//                viewPager = findViewById(R.id.viewPager);
//                viewPager.setCurrentItem(3);
//                break;
            default:
                super.onClick(v);
                break;
        }
    }

    private void resetBtn() {
        for (int i = 0; i < tvs.size(); ++i) {
            int id = getResources().getIdentifier("tv" + i, "id", getPackageName());
            TextView tv = tvs.get(i);
            if (i == mCurrentItem) {
                if (mCurrentStyle == 3) {
                    tv.setBackgroundResource(0);
                } else {
                    tv.setBackgroundResource(R.drawable.shape_fcbf24_18);
                    tv.setTextColor(ContextCompat.getColor(this, com.common.lib.R.color.text_color_7));
                }
            } else {
                tv.setBackground(null);
                if (mCurrentStyle != 3) {
                    tv.setTextColor(ContextCompat.getColor(this, com.common.lib.R.color.text_color_1));
                }
            }
        }
    }

    @Override
    public boolean isMainActivity() {
        return false;
    }

    @Override
    public void getVideoListSuccess(ProceduralTrainBean bean) {
        proceduralTrainBean = bean;
        initItem(bean);
    }
    public void initItem(ProceduralTrainBean bean){
        if(tvs == null){
            tvs = new ArrayList<>();
            tvs.add(findViewById(R.id.tv0));
        }
        mFragments = new ArrayList<>();
        mFragments.add(ProceduralTrainingListFragment.getInstance(0));
        System.out.println(" ddddddddddddddddddddddd  typelist : "+bean.getTypelist().size());
        if(bean.getTypelist() != null && bean.getTypelist().size()>0){
            for (int i=0;i<bean.getTypelist().size();i++){
                if(DataManager.Companion.getInstance().getRegion() == 0){
                    addTextView(bean.getTypelist().get(i).getName());
                }else {
                    addTextView(bean.getTypelist().get(i).getEnname());
                }
                mFragments.add(ProceduralTrainingListFragment.getInstance(bean.getTypelist().get(i).getId()));
            }

        }

        ViewPager viewPager = findViewById(R.id.viewPager);
        viewPager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
            @NonNull
            @Override
            public Fragment getItem(int position) {
                return mFragments.get(position);
            }

            @Override
            public int getCount() {
                return mFragments.size();
            }

        });
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                mCurrentItem = position;
                resetBtn();

            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        viewPager.setCurrentItem(mCurrentItem);
        for (int i=0;i<tvs.size();i++){
            int finalI = i;
            tvs.get(i).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    viewPager.setCurrentItem((finalI));
                }
            });
        }
    }

    public void addTextView(String info){
        TextView textView = new TextView(getContext());
        textView.setTextSize(12);
        textView.setTextColor(getColor(com.common.lib.R.color.text_color_11));
        textView.setGravity(Gravity.CENTER);
        textView.setText(info);
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.MATCH_PARENT);
        params.weight = 1.0f;
        textView.setLayoutParams(params);
        ll.addView(textView);
        tvs.add(textView);
    }

}
